package p.ithorns.integration.hazelcast.mqtt;

import lombok.*;
import p.ithorns.framework.core.model.IType;
import p.ithorns.framework.common.utils.ByteUtil;

/**
 * Payload
 * <p>
 * 数据负载
 *
 * @author liuhaitao
 * @version 1.0
 * @date 2020/3/27 22:57
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public final class Payload implements BaseRaw, Comparable<Payload> {

    /**
     * 指令码
     */
    private IType code;

    /**
     * 设备ID
     */
    private String devId;

    /**
     * 时间戳
     */
    private long timestamp;

    /**
     * 序列号/帧计数
     */
    private int seq;

    /**
     * 源数据
     */
    private byte[] raw;


    @Override
    public String toString() {
        return "{" +
                "devId:" + devId +
                ", timestamp:" + timestamp +
                ", code:" + code +
                ", seq:" + seq +
                ", raw:" + ByteUtil.bytes2HexStr(raw) +
                '}';
    }

    @Override
    public int compareTo(Payload target) {
        return Integer.compare(this.seq, target.getSeq());
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + seq;
        result = 31 * result + (null != code ? code.hashCode() : 0);
        result = 31 * result + (devId != null ? devId.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Payload)) {
            return false;
        }

        Payload payload = (Payload) obj;

        if (this == payload) {
            return true;
        }

        return this.devId.equals(payload.getDevId())
                && this.code == payload.getCode()
                && this.seq == payload.getSeq();
    }

    @Override
    public String getId() {
        return devId;
    }

}
